package com.jishuo.nontax.bfc.service;

import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.ErrorCodeEnum;
import com.jishuo.base.config.NonTaxConfig;
import com.jishuo.base.service.SequenceService;
import com.jishuo.nontax.bean.bank.Unit;
import com.jishuo.nontax.bean.bank.UnitXml;
import com.jishuo.nontax.bean.vo.UnitVO;
import com.jishuo.nontax.bfc.dao.UnitMapper;
import com.jishuo.nontax.bfc.tlq.MessageFactory;
import com.jishuo.nontax.bfc.tlq.MessageService;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther dinggaofei
 * @Date 2022/12/15 11:11
 */

@Service
public class UnitService {
    private static Logger logger = LoggerFactory.getLogger(UnitService.class);
    @Resource
    private UnitMapper unitMapper;
    @Resource
    private MessageService messageService;
    @Resource
    private SequenceService sequenceService;
    @Resource
    NonTaxConfig nonTaxConfig;


    public List<UnitVO> findUnitView(Long cantonid) {
        return unitMapper.findUnitVO(cantonid);
    }

    public List<Unit> findUnitViewTwo(Long cantonid) {
        return unitMapper.findUnit(cantonid);
    }

    /**
     * @param cantonid 行政区划ID
     * @auther dinggaofei
     * @Date 2022/12/13 14:03
     * @description 获取可用的预算单位信息给代理银行的业务场景
     */
    public BaseResult findUnit(Long cantonid) {

        try {
            //xml解析器
            Serializer serializer = new Persister();
            //xml转换器
            StringWriter sw = new StringWriter();
            ArrayList<Unit> list = (ArrayList<Unit>) unitMapper.findUnit(cantonid);
            //查询返回记录
            if (list.size()>0){
                UnitXml xml = new UnitXml();
                xml.setUnit(list);
                serializer.write(xml, sw);
                return new BaseResult(sw.toString());
            } else {
                return new BaseResult("<bills></bills>");
            }
        } catch (Exception e) {
            logger.info("失败：" + e.toString());
            return new BaseResult(ErrorCodeEnum.UNKNOWN_ERR, "<bills></bills>");
        }
    }

    /**
     * @param cantonid 行政区划ID
     * @param unitcode 单位(部门)编码
     * @auther dinggaofei
     * @Date 2022/12/13 14:25
     * @description 获取可用的预算单位信息给代理银行的业务场景
     */
    public BaseResult findUnitByUnitCode(String cantonid, String unitcode) {
        //xml解析器
        Serializer serializer = new Persister();
        //xml转换器
        StringWriter sw = new StringWriter();
        ;
        try {
            ArrayList<Unit> list = unitMapper.findUnitByCode(cantonid, unitcode);
            //查询返回记录
            if (list != null && list.size() > 0) {
                UnitXml xml = new UnitXml();
                xml.setUnit(list);
                serializer.write(xml, sw);
                return new BaseResult(sw.toString());
            }
            return new BaseResult("<bills></bills>");

        } catch (Exception e) {
            logger.info("失败：" + e.toString());
            return new BaseResult(ErrorCodeEnum.UNKNOWN_ERR, "<bills></bills>");
        }
    }

    /**
     * @param cantonid
     * @param unitcode
     * @auther dinggaofei
     * @Date 2022/12/15 13.35
     * @description 向财政主动获取预算单位
     */

    public BaseResult selBaseUnit(String cantonid, String unitcode) {

        try {
            long messageid = sequenceService.getNextSeq("ELEMESSAGESEQ");

            //xml解析器
            Serializer serializer = new Persister();
            //xml转换器
            StringWriter sw = new StringWriter();
            UnitXml xml = new UnitXml();
            //发送到财政
            boolean suc = messageService.sendAndRecvMessage("GETBASEUNIT", messageid, MessageFactory.getGetBaseUnit(cantonid,unitcode), "2.3");
            if (suc) {
                //获取缴款书
                ArrayList<Unit> list = unitMapper.findUnitByCode(cantonid, unitcode);
                //查询返回记录
                if (list != null && list.size() > 0) {
                    xml.setUnit(list);
                    serializer.write(xml, sw);
                    return new BaseResult(sw.toString());
                }
            }
            return new BaseResult("<bills></bills>");
        } catch (Exception e) {
            logger.info("处理失败：" + e.toString());
            return new BaseResult(ErrorCodeEnum.UNKNOWN_ERR, "<bills></bills>");
        }
    }

    //向财政主动获取预算单位
    public BaseResult getBaseUnit(int cantonid,String unitcode){
        try {
            long messageid = sequenceService.getNextSeq("ELEMESSAGESEQ");

            Object baseUnit = MessageFactory.getBaseUnit(cantonid, unitcode);
            //发送到财政
            messageService.sendMessage("GETBASEUNIT", messageid, baseUnit, "2.3");

            return new BaseResult();
        } catch (Exception e) {
            logger.info("处理失败："+e.toString());
            return new BaseResult(ErrorCodeEnum.UNKNOWN_ERR);
        }
    }

    public UnitVO selectUnitById(String id) {
        return unitMapper.selectUnitById(id);
    }
}
